package com.example.fw;

import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public class HelperWithWebDriverBase {
	
	protected final ApplicationManager manager;
	protected WebDriver driver;

	public HelperWithWebDriverBase (ApplicationManager manager) {
		this.manager = manager;
		driver = manager.getWebDriverHelper().getDriver();
	}
	
	public boolean isElementPresent(By locator) {
		driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
		boolean verdict = driver.findElements(locator).size() > 0;
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		return verdict;
	}
	
	protected void type(By locator, String enterTextInField) {
		driver.findElement(locator).clear();
		driver.findElement(locator).sendKeys(enterTextInField);
	}
	
	protected void select(By locator, String selectValue) {
		new Select(driver.findElement(locator)).selectByVisibleText(selectValue);
	}

	protected void selectRandom(By locator, String chooseGroup) {
		Select select = new Select(driver.findElement(locator));
		List<WebElement> options = select.getOptions();
		if (options.size() > 0) {
			int index = new Random().nextInt(options.size());
			options.get(index).click();
		}
	}
 
	protected WebElement findElement(By locator) {
		return driver.findElement(locator);
	}

	protected List<WebElement> findElements(By locator) {
		return driver.findElements(locator);
	}

	protected void openUrl(String urlForOpening) {
		driver.get(urlForOpening);
	}

	protected void click(By locator) {
		findElement(locator).click();
	}
	
}